/*
   Copyright 2022 cmanlh

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/**
 * date display
 */
import JscConfig from './config.js';
import JscDatabind from './databind.js';
import JscUtil from './util.js';
export default class JScMenutree extends JscDatabind {
    static get observedAttributes() { return [] }

    #jscGetUrl = null;
    #jscMenuTree = null;
    #jscMenuTreeScrollbox = null;
    #jqcScrollUl = null;
    #data = null

    constructor() {
        super();
        this.#init();
        this.#render();
    }

    connectedCallback() {
        this.reRender()
        this.#bindEventMenuTree()
    }

    disconnectedCallback() {
    }

    attributeChangedCallback(name, oldValue, newValue) {
    }

    #init() {
        let self = this
        this.#jscGetUrl = this.getAttribute('jscGetUrl');
        if (this.#jscGetUrl) {
            this.#data = JscUtil.get('user/getCurrentUser');
        }
        this.#data = [{id:'92bd9355d00c4f9c94132990110109d1',name:'学习天地',subMenuList:[{id:'92bd9355d00c4f9c94132990110109d1',name:'学习天地1'},{id:'92bd9355d00c4f9c94132990110109d1',name:'学习天地2'}]},{id:'92bd9355d00c4f9c94132990110109d1',name:'学习天地',subMenuList:[{id:'92bd9355d00c4f9c94132990110109d1',name:'学习天地1'},{id:'92bd9355d00c4f9c94132990110109d1',name:'学习天地2'},]}]
    }

    #render() {
        const shadowRoot = this.attachShadow({ mode: 'open' });
        shadowRoot.innerHTML = `
        <link rel='stylesheet' type='text/css' href='${JscConfig.cssUrl}'/>
        <div class='jscMenuTree'>
            <div class='jqcMenuTreeSearchList'></div>
            <div class='jscMenuTreeSearchBox'></div>
            <div class='jscMenuTreeFakeScroll'>
                <div class='jscMenuTreeScrollbox'>
                </div>
            </div>
            <div class='jscMenuTreeSwitch'></div>
            <div class='jscMenuTreeLimit'></div>
        <div>
        `;
        this.#jscMenuTree = this.shadowRoot.querySelector('.jscMenuTree');
        this.#jscMenuTreeScrollbox = this.shadowRoot.querySelector('.jscMenuTreeScrollbox');
    }
     // 事件绑定
    #bindEventMenuTree() {
        let self = this;
    }
    #toggleSlide(ele,s) {
        var eleHeight = ele.offsetHeight;
        // ele.style.transition = 'transform ' + s +'s';
        ele.style.overflow = 'hidden';
        if (eleHeight) {
            ele.style.display = 'none'
        } else {
            ele.style.display = 'block'
        }
    }
    reRender() {
        this.#jscMenuTreeScrollbox.appendChild(this.reRenderBox(this.#data))
    }
    renderMenu(data) {
        let self = this
        let jscMenuTreeLi = document.createElement('li')
        let jscMenuTreeLiDiv = document.createElement('div')
        // jscMenuTreeLiDiv.setAttribute('title',data.tips ? data.tips : data[_label])
        jscMenuTreeLiDiv.innerHTML = data.name
        jscMenuTreeLi.appendChild(jscMenuTreeLiDiv)
        jscMenuTreeLi.setAttribute('menuId',data.id)
        if (data.subMenuList && data.subMenuList.length >0) {
            jscMenuTreeLi.appendChild(this.reRenderBox(data.subMenuList))
            jscMenuTreeLi.classList.add('hasChild')
            jscMenuTreeLi.addEventListener('click', function (e) {
                if (this.querySelector('ul')) {
                    this.classList.add('active')
                    self.#toggleSlide(this.querySelector('ul'),500)
                } else {
                    // $.openPage(itemUrl, openName);
                }
            });
        }
        return jscMenuTreeLi
    }
    reRenderBox(data) {
        let self = this
        let jqcScrollUl = document.createElement('ul')
        jqcScrollUl.classList.add('jqcMenuTreeItem')
        if (data && data.length > 0) {
            for(let i = 0;i < data.length; i++) {
                jqcScrollUl.appendChild(self.renderMenu(data[i]))
            }
        }
        return jqcScrollUl
    }
}

if (!customElements.get('jsc-menu-tree')) {
    window.customElements.define("jsc-menu-tree", JScMenutree);
}